package com.tobie.userservice.service.impl;

import com.tobie.userservice.dao.UserMapper;
import com.tobie.userservice.dao.UserRoleMapper;
import com.tobie.userservice.pojo.dto.UserDto;
import com.tobie.userservice.pojo.dto.UserRole;
import com.tobie.userservice.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Tobieance
 * @description
 * @date 2023-11-16 16:00
 */
@Service
@org.apache.dubbo.config.annotation.Service
@Slf4j
public class UserServiceImpl implements IUserService {

    @Resource
    UserMapper userMapper;

    @Resource
    UserRoleMapper userRoleMapper;

    /**
     * 用户注册
     *
     * @param userDto 用户数据传输实体
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = {})
    public boolean registerUser(UserDto userDto) {
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        String password=bCryptPasswordEncoder.encode(userDto.getPassword());
        userDto.setPassword(password);
        log.info(userDto.toString());
        UserRole userRole=new UserRole();
        if(userMapper.insert(userDto) > 0){
            userRole.setUserId(userDto.getUserId());
            userRole.setRoleId(2);
        }
        return userRoleMapper.insert(userRole)>0;
    }
}
